package com.coffee.admin.controller.platform;

import com.alibaba.fastjson.JSONObject;
import com.coffee.admin.api.merchant.query.MerchantIntegralLogQuery;
import com.coffee.admin.api.merchant.result.MerchantIntegralLogResult;
import com.coffee.admin.api.merchant.service.MerchantIntegralLogService;
import com.coffee.admin.controller.BaseController;
import com.coffee.admin.excel.DailiOrderListResultExport;
import com.coffee.admin.excel.OrderBalanceResultExcel;
import com.coffee.admin.excel.SkuOrderResultExcel;
import com.coffee.admin.utils.WXPayUtil;
import com.coffee.core.anotation.OperLog;
import com.coffee.core.anotation.PlatformLoginCheck;
import com.coffee.core.anotation.SupplierLoginCheck;
import com.coffee.core.entity.LoginUser;
import com.coffee.core.entity.Page;
import com.coffee.core.entity.PagerInfo;
import com.coffee.core.enums.RoleTypeEnum;
import com.coffee.core.enums.SkuTypeEnum;
import com.coffee.core.exception.BusinessException;
import com.coffee.core.result.Result;
import com.coffee.core.util.BeanUtils;
import com.coffee.core.util.DTOUtils;
import com.coffee.core.util.ExcelUtiles;
import com.coffee.daili.api.result.DailiOrderListResult;
import com.coffee.order.api.inquire.req.InquireOrderTestingReq;
import com.coffee.order.api.inquire.req.MerchantIntegralLogReq;
import com.coffee.order.api.inquire.req.SupplierOrderListReq;
import com.coffee.order.api.inquire.result.InquireOrderTestingResult;
import com.coffee.order.api.inquire.result.SupplierOrderResult;
import com.coffee.order.api.inquire.result.SupplierSubOrderListResult;
import com.coffee.order.api.inquire.service.InquireOrderService;
import com.coffee.order.api.inquire.service.InquireOrderTestingService;
import com.coffee.order.api.sku.query.OrderBalanceQuery;
import com.coffee.order.api.sku.req.PlatformOrderListReq;
import com.coffee.order.api.sku.result.*;
import com.coffee.order.api.sku.service.OrderBalanceService;
import com.coffee.order.api.sku.service.SkuOrderService;
import org.apache.dubbo.config.annotation.DubboReference;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/platform")
public class PlatformOrderController extends BaseController {
    private final static Logger logger = LoggerFactory.getLogger(PlatformOrderController.class);
    @DubboReference(check = false)
    private SkuOrderService skuOrderService;
    @DubboReference(check = false)
    private InquireOrderService inquireOrderService;
    @DubboReference(check = false)
    private InquireOrderTestingService inquireOrderTestingService;
    @DubboReference(check = false)
    private MerchantIntegralLogService merchantIntegralLogService;
    @DubboReference(check = false)
    private OrderBalanceService orderBalanceService;

    @Value("${parameter.adminWebDomain}")
    private String webDomain;

    @OperLog(operModul = "平台-订单管理-订单管理", operDesc = "分页查询订单管理列表")
    @PlatformLoginCheck(alias = "PlatformOrderListAlias")
    @RequestMapping(value = "/order/findList", method = {RequestMethod.POST})
    public Result<Page<SkuOrderResult>> findList(HttpServletRequest request, @RequestBody PlatformOrderListReq req) {
        Result<Page<SkuOrderResult>> result = Result.success(null);
        PagerInfo pagerInfo = setDefaultPageInfo(req.getPageSize(), req.getPageIndex());
        try {
            req.setNoType(SkuTypeEnum.INQUIRE.getCode());
            result.setData(skuOrderService.getOrderListResultList(req, pagerInfo));
        } catch (BusinessException e) {
            e.printStackTrace();
        }
        return result;

    }


    @OperLog(operModul = "服务商-订单管理", operDesc = "订单管理导出")
    @PlatformLoginCheck(alias = "PlatformOrderListAlias")
    @RequestMapping(value = "/order/findList/export", method = {RequestMethod.POST})
    public void export(HttpServletResponse response, @RequestBody PlatformOrderListReq req) {
        PagerInfo pagerInfo = setDefaultPageInfo(Integer.MAX_VALUE, 1);
        try {
            req.setNoType(SkuTypeEnum.INQUIRE.getCode());
            List<SkuOrderResultExport> orderListResultListExport = skuOrderService.getOrderListResultListExport(req, pagerInfo);
            logger.info("orderListResultListExport=====" + JSONObject.toJSONString(orderListResultListExport));
            // 导出Excel文件
            List<SkuOrderResultExcel> excelList = DTOUtils.convertList(orderListResultListExport, SkuOrderResultExcel.class);
            ExcelUtiles.exportExcel(excelList, "订单列表", "sheet1", SkuOrderResultExcel.class,
                    "order.xls", true, response);
        } catch (BusinessException e) {
            e.printStackTrace();
        }

    }


    @OperLog(operModul = "平台-订单管理-订单详情", operDesc = "订单详情")
    @PlatformLoginCheck(alias = "PlatformOrderListAlias")
    @RequestMapping(value = "/order/detail")
    public Result<SkuOrderResult> detail(HttpServletRequest request, @RequestBody JSONObject jsonObject) {
        Integer id = jsonObject.getInteger("skuOrderId");
        Result<SkuOrderResult> result = Result.success(null);
        SkuOrderResult detail = null;
        try {
            detail = skuOrderService.getSkuOrderResultDetailBySupplier(id, getSupplier().getSupplierId(), RoleTypeEnum.PLATFORM.getCode());
        } catch (BusinessException e) {
            e.printStackTrace();
            return Result.failed(e.getCode(), e.getMessage());
        }
        result.setData(detail);
        return result;
    }

    @OperLog(operModul = "供应商-订单管理-订单列表", operDesc = "查询订单列表")
    @PlatformLoginCheck(alias = "PlatformInquireOrderListAlias")
    @RequestMapping(value = "/inquire/order/list", method = {RequestMethod.POST})
    public Result<Page<SupplierSubOrderListResult>> orderList(HttpServletRequest request, @RequestBody SupplierOrderListReq req) {
        Result<Page<SupplierSubOrderListResult>> result = Result.success(null);
        PagerInfo pagerInfo = setDefaultPageInfo(req.getPageSize(), req.getPageIndex());
        Page<SupplierSubOrderListResult> supplierSubOrderListResultPage = inquireOrderService.pageSupplierSubOrderListResult(req, pagerInfo);
        result.setData(supplierSubOrderListResultPage);
        return result;
    }


    @OperLog(operModul = "供应商-订单管理-订单详情", operDesc = "查询订单详情")
    @PlatformLoginCheck(alias = "PlatformInquireOrderListAlias")
    @RequestMapping(value = "/inquire/order/detail", method = {RequestMethod.POST})
    public Result<SupplierOrderResult> orderDetail(@RequestBody JSONObject jsonObject, HttpServletRequest request) {
        Integer inquireOrderId = jsonObject.getInteger("inquireOrderId");
        LoginUser supplier = getSupplier();
        Result<SupplierOrderResult> result = Result.success(null);
        SupplierOrderResult supplierOrderResult = null;
        try {
            supplierOrderResult = inquireOrderService.getSupplierOrderResult(inquireOrderId, supplier.getSupplierId(), RoleTypeEnum.PLATFORM.getCode());
        } catch (BusinessException e) {
            return Result.failed(e.getCode(), e.getMessage());
        }
        result.setData(supplierOrderResult);
        return result;
    }

    @OperLog(operModul = "供应商-订单管理-检测订单列表", operDesc = "检测订单列表")
    @PlatformLoginCheck(alias = "PlatformTestOrderListAlias")
    @RequestMapping(value = "/inquire/test/order/list", method = {RequestMethod.POST})
    public Result<Page<InquireOrderTestingResult>> testOrderList(@RequestBody InquireOrderTestingReq req, HttpServletRequest request) {
        LoginUser supplier = getSupplier();
        PagerInfo pagerInfo = setDefaultPageInfo(req.getPageSize(), req.getPageIndex());
        Result<Page<InquireOrderTestingResult>> result = Result.success(null);
        Page<InquireOrderTestingResult> pagerByReq = inquireOrderTestingService.getPagerByReq(req, pagerInfo);
        result.setData(pagerByReq);
        return result;
    }

    @OperLog(operModul = "供应商-订单管理-检测订单详情", operDesc = "检测订单列表")
    @PlatformLoginCheck(alias = "PlatformTestOrderListAlias")
    @RequestMapping(value = "/inquire/test/order/detail", method = {RequestMethod.POST})
    public Result<InquireOrderTestingResult> testOrderDetail(@RequestBody JSONObject req, HttpServletRequest request) {
        Result<InquireOrderTestingResult> result = Result.success(null);
        Integer id = req.getInteger("id");
        InquireOrderTestingResult detailById = inquireOrderTestingService.getDetailById(id, null);
        result.setData(detailById);
        return result;
    }

    @OperLog(operModul = "供应商-订单管理-积分流水", operDesc = "积分流水")
    @PlatformLoginCheck(alias = "PlatformIntegralLogListAlias")
    @RequestMapping(value = "/integral/log/list", method = {RequestMethod.POST})
    public Result<Page<MerchantIntegralLogResult>> merchantIntegralLog(@RequestBody MerchantIntegralLogReq req, HttpServletRequest request) {
        Result<Page<MerchantIntegralLogResult>> result = Result.success(null);
        MerchantIntegralLogQuery param = new MerchantIntegralLogQuery();
        BeanUtils.copyProperties(param, req);
        PagerInfo pagerInfo = setDefaultPageInfo(req.getPageSize(), req.getPageIndex());
        Page<MerchantIntegralLogResult> pager = merchantIntegralLogService.getPager(param, pagerInfo);
        result.setData(pager);
        return result;
    }

    @OperLog(operModul = "平台-订单管理-余额明显管理", operDesc = "分页查询余额明显管理列表")
    @PlatformLoginCheck(alias = "PlatformOrderListAlias")
    @RequestMapping(value = "/orderBalance/findList", method = {RequestMethod.POST})
    public Result<Page<OrderBalanceBackendResult>> orderBalanceFindList(HttpServletRequest request, @RequestBody OrderBalanceQuery req) {
        Result<Page<OrderBalanceBackendResult>> result = Result.success(null);
        PagerInfo pagerInfo = setDefaultPageInfo(req.getPageSize(), req.getPageIndex());
        result.setData(orderBalanceService.getOrderBalanceBackendResult(req, pagerInfo));
        return result;

    }


    @OperLog(operModul = "服务商-余额明显管理", operDesc = "余额明显管理导出")
    @PlatformLoginCheck(alias = "PlatformOrderListAlias")
    @RequestMapping(value = "/orderBalance/findList/export", method = {RequestMethod.POST})
    public void orderBalanceExport(HttpServletResponse response, @RequestBody OrderBalanceQuery req) {
        PagerInfo pagerInfo = setDefaultPageInfo(Integer.MAX_VALUE, 1);
        Page<OrderBalanceBackendResult> orderBalanceBackendResult = orderBalanceService.getOrderBalanceBackendResult(req, pagerInfo);
        // 导出Excel文件
        List<OrderBalanceResultExcel> excelList = DTOUtils.convertList(orderBalanceBackendResult.getData(), OrderBalanceResultExcel.class);
        ExcelUtiles.exportExcel(excelList, "余额明细列表", "sheet1", OrderBalanceResultExcel.class,
                "order.xls", true, response);
    }

}
